/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.feed.impl;

import java.io.IOException;

import static com.semagia.atomico.IConstants.REL_SELF;

import com.semagia.atomico.dm.IAuthor;
import com.semagia.atomico.dm.IAuthorAwareEntity;
import com.semagia.atomico.dm.IEntity;
import com.semagia.atomico.dm.ISummaryAwareEntity;
import com.semagia.atomico.feed.IFeedHandler;
import com.semagia.atomico.server.IServerConstants;

/**
 * Common functions for feed writers.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 * @version $Rev: 117 $ - $Date: 2010-11-06 14:39:35 +0000 (Sat, 06 Nov 2010) $
 */
final class FeedWriterUtils implements IServerConstants {

    private FeedWriterUtils() {
        // noop.
    }

    /**
     * Issues a {@link IFeedHandler#startFeed(String, String, long)}, a
     * {@link IFeedHandler#author(IAuthor)}, 
     * a {@link IFeedHandler#generator(String, String)} and 
     * a {@link IFeedHandler#link(String, String)} notification.
     *
     * @param handler The feed handler which receives the events.
     * @param feed The feed.
     * @throws IOException In case of an error.
     */
    public static void writeFeedStart(final IFeedHandler handler, final IFeed<?> feed) throws IOException {
        handler.startFeed(feed.getId(), feed.getTitle(), feed.getUpdated());
        handler.generator(APP_NAME, APP_IRI);
        handler.link(feed.getId(), REL_SELF);
        _writeAuthors(handler, feed.getAuthors());
        _writeSummary(handler, feed.getSummary());
    }

    /**
     * Issues a {@link IFeedHandler#startEntry(String, String, long)} event.
     *
     * @param handler The feed handler which receives the events.
     * @param entry The entry.
     * @throws IOException In case of an error.
     */
    public static void writeEntryStart(final IFeedHandler handler, final IEntity entry) throws IOException {
        handler.startEntry(entry.getId(), entry.getTitle(), entry.getUpdated());
        if (entry instanceof IAuthorAwareEntity) {
            _writeAuthors(handler, ((IAuthorAwareEntity)entry).getAuthors());
        }
        if (entry instanceof ISummaryAwareEntity) {
            _writeSummary(handler, ((ISummaryAwareEntity) entry).getSummary());
        }
    }

    /**
     * Issues {@link IFeedHandler#author(IAuthor)} events for all
     * authors.
     *
     * @param handler The feed handler which receives the events.
     * @param authors A maybe empty iterable of authors.
     * @throws IOException In case of an error.
     */
    private static void _writeAuthors(final IFeedHandler handler, 
            final Iterable<IAuthor> authors) throws IOException {
        for (IAuthor author: authors) {
            handler.author(author);
        }
    }

    /**
     * Issues a {@link IFeedHandler#summary(String)} event if the
     * summary is not null.
     *
     * @param handler The feed handler which receives the events.
     * @param summary A string or {@code null}.
     * @throws IOException In case of an error.
     */
    private static void _writeSummary(final IFeedHandler handler,
            final String summary) throws IOException {
        if (summary != null) {
            handler.summary(summary);
        }
    }

}
